package com.rqf.mr.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.mr.service.IPatientService;

@Service("PatientService")
public class PatientService implements IPatientService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Map<String, Object>> getPatisByBedId(String bedId) {
		if ( StringUtils.isEmpty(bedId)) {
			return null;
		}
		String sql = "select * from view_ehisinpati_info bb where bb.admitdate=(select max(pei.admitdate) from view_ehisinpati_info pei where  pei.BEDID='" + bedId
		+ "' and pei.LEAVEDATE is  null )";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> getInPatiidByOrdId(String ordid) {
		if ( StringUtils.isEmpty(ordid)) {
			return null;
		}
		String sql = "select distinct(piv.inpatiid) from view_ehis_pati_in_visit piv where piv.patiid="
				+ "(select pio.patiid from view_ehis_pati_in_order pio where pio.ordid='"+ordid+"')";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> getPatiByBeddesNum(String bedDes) {
		if ( StringUtils.isEmpty(bedDes)) {
			return null;
		}
		String sql = "select * from view_ehisinpati_info bb where bb.admitdate=(select max(pei.admitdate) from view_ehisinpati_info pei where  pei.beddes='" + bedDes
				+ "' and pei.LEAVEDATE is  null )";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	
	@Override
	public List<Map<String, Object>> getPatisbyinpatiid(String inpatiId) {
		if ( StringUtils.isEmpty(inpatiId)) {
			return null;
		}
		String sql = "select * from view_ehisinpati_info bb where bb.admitdate=( select max(pei.admitdate) from view_ehisinpati_info pei where pei.inpatiid= '" + inpatiId
				+ "' )  and  bb.inpatiid = '" + inpatiId + "'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> getPatiInfoByBedId(String bedId) {
		if ( StringUtils.isEmpty(bedId)) {
			return null;
		}
		String sql = "select * from view_ehisinpati_info pib where pib.patiid = (select pp.patiid from view_ehis_pati_in_visit  pp where pp.admitdate =(select  max(dd.admitdate) from view_ehis_pati_in_visit dd where  dd.bedid = '"
				+ bedId + "')) and pib.admitdate =(select  max(dd.admitdate) from view_ehis_pati_in_visit dd where  dd.bedid = '" + bedId + "'  and dd.leavedate is null)";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

}
